/**
 * Created with IntelliJ IDEA.
 * Description:在排序数组中查找元素的第一个和最后一个位置
 * User: bjc
 * Date: 2025-11-15
 * Time: 17:32
 */
public class Test02_searchRange {
    public int[] searchRange(int[] nums, int target) {
        if(nums.length == 0) return new int[]{-1,-1};
        //寻找区间的左端点
        int left = 0,right = nums.length-1;
        while(left < right){
            int mid = left +(right - left)/2;
            if(nums[mid] < target) left = mid + 1;
            else right = mid;
        }
        if(nums[left] != target) return new int[]{-1,-1};
        int left_index = left;
        //寻找区间的右端点
        left = 0;
        right = nums.length-1;
        while(left < right){
            int mid = left +(right - left)/2 + 1;
            if(nums[mid] > target) right = mid - 1;
            else left = mid;
        }
        int right_index = left;
        return new int[]{left_index,right_index};
    }
}
